/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.TransaksiParkirdanLaporan.Com.Transaksi;

import Login.Karyawan;
import Login.Koneksi;
import java.awt.TextField;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Tea-Nose
 */
public class DataParkir {

    private String noParkir;
    private String kode_karesidenan;
    private String kode_wilayah;
    private String no_polisi;
    private String no_polisi_kendaraan;
    private String kode_merkHelm_jenisHelm;
    private int jumlahHelm;
    private int biayaParkir;
    private String tanggalMasuk;
    private String tanggalKeluar;
    private String jamMasuk;
    private String jamKeluar;
    private String idKaryawan;
    private double biaya_parkir = 2000;
    private Connection conn;
    private static DataParkir koneksi;
    private String jamMasukShift;
    private String merkHelm;

    public DataParkir() {
    }

    public DataParkir(Connection conn) {
        this.conn = conn;
    }

    public static DataParkir getConnection() throws SQLException, IOException {
        if (koneksi == null) {
            koneksi = new DataParkir(Koneksi.getDBConnection());

        }
        return koneksi;
    }

    public List<DataParkir> getLaporanHarian(String tanggal) throws SQLException, Exception {

        PreparedStatement ps = null;
        ResultSet rs = null;

        String query = "select idkaryawan, nomorpolisi,jumlahhelm, biayaparkir from dataparkir"
                + " where to_char(tanggalkeluar,'dd-MM-yyyy')=(?)";

        ps = conn.prepareStatement(query);
        ps.setObject(1, tanggal);
        rs = ps.executeQuery();

        List<DataParkir> parkir = new ArrayList<DataParkir>();
        while (rs.next()) {

            DataParkir data = new DataParkir();
            data.setIdKaryawan(rs.getString(1));
            data.setNoPolisiKendaraan(rs.getString(2));
            data.setJumlahHelm(rs.getInt(3));
            data.setBiaya_parkir(rs.getInt(4));
            parkir.add(data);
        }
        return parkir;


    }

    /**
     * @return the noParkir
     */
    public String getNoParkir() {
        return noParkir;
    }

    /**
     * @param noParkir the noParkir to set
     */
    public void setNoParkir(String noParkir) {
        this.noParkir = noParkir;
    }

    /**
     * @return the noPolisiKendaraan
     */
    public String getNoPolisiKendaraan() {
        return no_polisi_kendaraan;
    }

    /**
     * @param noPolisiKendaraan the noPolisiKendaraan to set
     */
    public void setNoPolisiKendaraan(String noPolisiKendaraan) {
        this.no_polisi_kendaraan = noPolisiKendaraan;
    }

    /**
     * @return the kode_merkHelm_jenisHelm
     */
    public String getKode_merkHelm_jenisHelm() {
        return kode_merkHelm_jenisHelm;
    }

    /**
     * @param kode_merkHelm_jenisHelm the kode_merkHelm_jenisHelm to set
     */
    public void setKode_merkHelm_jenisHelm(String kode_merkHelm_jenisHelm) {
        this.kode_merkHelm_jenisHelm = kode_merkHelm_jenisHelm;
    }

    /**
     * @return the jumlahHelm
     */
    public int getJumlahHelm() {
        return jumlahHelm;
    }

    /**
     * @param jumlahHelm the jumlahHelm to set
     */
    public void setJumlahHelm(int jumlahHelm) throws Exception {
        if (jumlahHelm >= 0 && jumlahHelm <= 3) {
            this.jumlahHelm = jumlahHelm;
        } else {
            throw new Exception("Jumlah helm harus 0 - 3");
        }
    }

    /**
     * @return the tanggalMasuk
     */
    public String getTanggalMasuk() {
        return tanggalMasuk;
    }

    /**
     * @param tanggalMasuk the tanggalMasuk to set
     */
    public void setTanggalMasuk(String tanggalMasuk) {
        this.tanggalMasuk = tanggalMasuk;
    }

    /**
     * @return the tanggalKeluar
     */
    public String getTanggalKeluar() {
        return tanggalKeluar;
    }

    /**
     * @param tanggalKeluar the tanggalKeluar to set
     */
    public void setTanggalKeluar(String tanggalKeluar) {
        this.tanggalKeluar = tanggalKeluar;
    }

    /**
     * @return the jamMasuk
     */
    public String getJamMasuk() {
        return jamMasuk;
    }

    /**
     * @param jamMasuk the jamMasuk to set
     */
    public void setJamMasuk(String jamMasuk) {
        this.jamMasuk = jamMasuk;
    }

    /**
     * @return the jamKeluar
     */
    public String getJamKeluar() {
        return jamKeluar;
    }

    /**
     * @param jamKeluar the jamKeluar to set
     */
    public void setJamKeluar(String jamKeluar) {
        this.jamKeluar = jamKeluar;
    }

    /**
     * @return the idKaryawan
     */
    public String getIdKaryawan() {
        return idKaryawan;
    }

    /**
     * @param idKaryawan the idKaryawan to set
     */
    public void setIdKaryawan(String idKaryawan) {
        this.idKaryawan = idKaryawan;
    }

    /**
     * @return the kode_karesidenan
     */
    public String getKode_karesidenan() {
        return kode_karesidenan;
    }

    /**
     * @param kode_karesidenan the kode_karesidenan to set
     */
    public void setKode_karesidenan(String kode_karesidenan) throws Exception {
        String kode_karesidenan_pattern = "[a-zA-Z]{1,2}";
        if (kode_karesidenan.matches(kode_karesidenan_pattern)) {
            this.kode_karesidenan = kode_karesidenan.toUpperCase();
        } else if (kode_karesidenan.matches("")) {
            throw new Exception("Error\nField kode karesidenan belum diisi");
        } else {
            throw new Exception("Error\nKode karesidenan harus berupa 1 atau 2 digit karakter huruf");
        }
    }

    /**
     * @return the kode_wilayah
     */
    public String getKode_wilayah() {
        return kode_wilayah;
    }

    /**
     * @param kode_wilayah the kode_wilayah to set
     */
    public void setKode_wilayah(String kode_wilayah) throws Exception {
        String kode_wilayah_pattern = "[a-zA-Z]{1,3}";
        if (kode_wilayah.matches(kode_wilayah_pattern)) {
            this.kode_wilayah = kode_wilayah.toUpperCase();
        } else if (kode_karesidenan.matches("")) {
            throw new Exception("Error\nField kode wilayah belum diisi");
        } else {
            throw new Exception("Error\nKode wilayah harus berupa 1 hingga 3 karakter huruf");
        }
    }

    /**
     * @return the no_polisi
     */
    public String getNo_polisi() {
        return no_polisi;
    }

    /**
     * @param no_polisi the no_polisi to set
     */
    public void setNo_polisi(String no_polisi) throws Exception {
        String no_polisi_pattern = "[0-9]{1,4}";
        if (no_polisi.matches(no_polisi_pattern)) {
            this.no_polisi = no_polisi;
        } else if (no_polisi.matches("")) {
            throw new Exception("Error\nField no polisi belum diisi");
        } else {
            throw new Exception("Error\nInput no polisi harus berupa digit angka");
        }
    }

    public void simpanDataParkir(DataParkir dataParkir) throws SQLException {

        //Connection conn = Koneksi.getDBConnection();


        String sisip = "update dataparkir set  kodehelm=(?), jumlahhelm=(?), nomorpolisi=(?), tanggalmasuk=(?), jammasuk=(?),biayaparkir=(?),shiftmasuk=(?) where noparkir=(?)";

        PreparedStatement pstmt = conn.prepareStatement(sisip);

        pstmt.setObject(8, dataParkir.getNoParkir());

        pstmt.setObject(1, dataParkir.getKode_merkHelm_jenisHelm());
        pstmt.setObject(2, dataParkir.getJumlahHelm());
        pstmt.setObject(3, dataParkir.getNoPolisiKendaraan());
        pstmt.setObject(4, dataParkir.getTanggalMasuk());
        pstmt.setObject(5, dataParkir.getJamMasuk());

        pstmt.setObject(6, dataParkir.getBiaya_parkir());
        pstmt.setObject(7, dataParkir.getJamMasukShift());
        pstmt.executeUpdate();
        //conn.commit;




    }

    public boolean cariDataParkirKeluar(DataParkir dp) {
        boolean stat = false;
        try {
            PreparedStatement ps = null;
            ResultSet rs;

            String query = "select noparkir,kodehelm,jammasuk,to_char(tanggalmasuk,'dd-MM-yyyy') from dataparkir where nomorpolisi ='" + dp.getNoPolisiKendaraan() + "' AND jamkeluar is null";
            ps = conn.prepareStatement(query);
            rs = ps.executeQuery();
            while (rs.next()) {
                if (rs.getString(1) == null) {
                    stat = false;
                } else {
                    dp.setNoParkir(rs.getString(1));
                    dp.setKode_merkHelm_jenisHelm(rs.getString(2));
                    dp.setJamMasuk(rs.getString(3));
                    dp.setTanggalMasuk(rs.getString(4));
                    stat = true;
                }
            }
        } catch (Exception ex) {
            System.out.println("terjadi error");
        }
        return stat;
    }

    public boolean ifExist(DataParkir dp) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs;
        String query = "select nomorpolisi from dataparkir where noparkir ="
                + " '" + dp.getNoParkir() + "' and jamkeluar is null";
        ps = conn.prepareStatement(query);
        rs = ps.executeQuery();
        if (rs.next() == false) {
            return false;
        } else {
            return true;
        }
    }

    public void rubahDataParkir(DataParkir dp) throws SQLException {
        GregorianCalendar gc = new GregorianCalendar();
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss");
        String vTanggalKeluar = sdf1.format(gc.getTime());
        String vJamKeluar = sdf2.format(gc.getTime());
        PreparedStatement ps = null;
        ResultSet rs;
        String query = "UPDATE dataparkir set biayaparkir =2000, "
                + "jamkeluar='" + vJamKeluar + "', tanggalkeluar='" + vTanggalKeluar + "' "
                + "where noparkir='" + dp.getNoParkir() + "'";
        ps = conn.prepareStatement(query);
        ps.executeUpdate();
    }

    /**
     * @return the biaya_parkir
     */
    public double getBiaya_parkir() {
        return biaya_parkir;
    }

    /**
     * @param biaya_parkir the biaya_parkir to set
     */
    public void setBiaya_parkir(double biaya_parkir) {
        this.biaya_parkir = biaya_parkir;
    }

    public int jumlahKendaraanPerShift(String idStaff) {
        try {
            Connection conn = Koneksi.getDBConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;
            String hasil = null;
            String query = "select count (noparkir) from dataparkir where idstaff='" + idStaff + "'";
            ps = conn.prepareStatement(query);
            rs = ps.executeQuery();
            while (rs.next()) {
                hasil = (rs.getString(1));
            }
            return Integer.parseInt(hasil);
        } catch (Exception ex) {
            System.out.println("terjadi error");
            return 0;
        }
    }

    public double jumlahPendapatanPerShift(String idStaff) {
        try {
            PreparedStatement ps = null;
            ResultSet rs = null;
            String hasil = null;
            String query = "select count (noparkir) from dataparkir where idstaff='" + idStaff + "'";
            ps = conn.prepareStatement(query);
            rs = ps.executeQuery();
            while (rs.next()) {
                hasil = (rs.getString(1));
            }
            return Integer.parseInt(hasil) * biaya_parkir;
        } catch (Exception ex) {
            System.out.println("terjadi error");
            return 0;
        }
    }

    public String idParkir() throws SQLException, FileNotFoundException, IOException {
//        String query = "";
        String sql = "call noparkir(?,?)";
        CallableStatement callst = conn.prepareCall(sql);
        callst.setObject(1, Karyawan.getusrID().getUsername());
        callst.registerOutParameter(2, java.sql.Types.VARCHAR);
        callst.execute();
        String noParkir = callst.getString(2);
        return noParkir;


    }

    public void deleteDataParkir(String noParkir) {
        try {
            String query = "delete from dataparkir where noparkir='" + noParkir + "'";
            PreparedStatement pstmt = conn.prepareStatement(query);
            pstmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DataParkir.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * @return the jamMasukShift
     */
    public String getJamMasukShift() {
        return jamMasukShift;
    }

    /**
     * @param jamMasukShift the jamMasukShift to set
     */
    public void setJamMasukShift(String jamMasukShift) {
        this.jamMasukShift = jamMasukShift;
    }

    /**
     * @return the merkHelm
     */
    public String getMerkHelm() {
        return merkHelm;
    }

    /**
     * @param merkHelm the merkHelm to set
     */
    public void setMerkHelm(String merkHelm) throws Exception {
        String merk_helm_pattern = "[a-zA-Z0-9]{3}";
        if (merkHelm.matches(merk_helm_pattern)) {
            this.merkHelm = merkHelm;
        } else if (merkHelm.matches("")) {
            throw new Exception("Field merk helm masih ada yang kosong");
        } else {
            throw new Exception("Inputan merk helm merupakan karakter angka dan huruf dan 3 huruf dari merk aslinya");
        }
    }
}
